import ReactDOM from 'react-dom'
import React, { Component } from 'react'

// 类组件定义状态
class App extends Component {

  state = {
    count: 0
  }

  // click的处理函数
  handleClick(e) {
    console.log(e)
    console.log(this)
  }

  render() {
    return (
      <div>
        <div>计数器：{this.state.count}</div>
        {/* 将this.handleClick赋值给onClick，调用函数通过onClick，所以此时的this是undefined */}
        <button onClick={this.handleClick}>+1</button>
      </div>
    )
  }
}


// 推导this是undefined的原因
const obj = {
  name: 'tom',
  say() {
    console.log(this)
  }
}
obj.say() // this ===> obj
const say2 = obj.say
say2()    // this ===> undefined


ReactDOM.render(<App />, document.getElementById('root'))